//
//  YSLLoginRegisterViewController.m
//  iOS-百思不得哥
//
//  Created by lanou on 16/7/8.
//  Copyright © 2016年 杨斯霖. All rights reserved.
//

#import "YSLLoginRegisterViewController.h"

@interface YSLLoginRegisterViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;

@property (weak, nonatomic) IBOutlet UIImageView *backgroundView; //背景色

//登录框距离控制器View左边的间隙

@property (weak, nonatomic) IBOutlet YSLTextField *loginphone;

@property (weak, nonatomic) IBOutlet YSLTextField *loginpassword;

@property (weak, nonatomic) IBOutlet YSLTextField *registerPhone;

@property (weak, nonatomic) IBOutlet YSLTextField *registerPassword;

@property (nonatomic, strong)MBProgressHUD *hub;
@end

@implementation YSLLoginRegisterViewController



//懒加载
- (MBProgressHUD *)hub {
    
    if (!_hub) {
        _hub = [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:_hub];
    }
    return _hub;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view insertSubview:self.backgroundView atIndex:0]; //不知道什么原因背景图片把其他控件覆盖了,因此将背景色插入最里边(可能是因为约束问题)
    
    self.loginphone.delegate = self;
    self.loginpassword.delegate = self;
    self.registerPhone.delegate = self;
    self.registerPassword.delegate = self;
  
}

#pragma mark -  登录事件
- (IBAction)loginAction:(id)sender {
    
    [BmobUser loginInbackgroundWithAccount:self.loginphone.text andPassword:self.loginpassword.text block:^(BmobUser *user, NSError *error) {
        if (user) {

            [self BackButton];
        } else {
       
            [self showAlertViewWithMessage:@"账号或密码错误"];
        }
    }];
}

- (void)showAlertViewWithMessage:(NSString *)message {
#pragma mark - 消除警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [[[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
#pragma clang diagnostic pop
    
}


#pragma mark - 注册事件
- (IBAction)registerAction:(UIButton *)sender {
   
    BmobUser *bUser = [[BmobUser alloc] init];
    [bUser setPassword:self.registerPassword.text];
    bUser.username = self.registerPhone.text;
    bUser.email = self.registerPhone.text;

    [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        if (isSuccessful){

            [self showAlertViewWithMessage:@"注册成功!"];
        } else {
            NSLog(@"%@",error.userInfo);
            
            if ([error.userInfo[@"NSLocalizedDescription"] containsString:@"taken"]) {
                [self showAlertViewWithMessage:@"账号已存在"];
            }else if ([error.userInfo[@"NSLocalizedDescription"] containsString:@"valid email"])
                [self showAlertViewWithMessage:@"邮箱格式错误!"];
        }
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - 注册帐号点击
- (IBAction)showLoginOrRegister:(UIButton *)button {

    if ( self.scrollView.contentOffset.x != 0) {
        [UIView animateWithDuration:0.25 animations:^{
            self.scrollView.contentOffset = CGPointZero;
            [self.leftBtn setTitle:@"注册账号" forState:UIControlStateNormal];
        }];
        
  
    }else {
        [UIView animateWithDuration:0.25 animations:^{
            self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
            [self.leftBtn setTitle:@"登录账号" forState:UIControlStateNormal];
        }];
    }
    
}

#pragma  mark - 退出按钮
- (IBAction)BackButton
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma - 自动登录


#pragma mark - 状态栏颜色 让控制器对应的状态栏是白色
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}



- (IBAction)clickForgetPwd:(id)sender {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"找回密码" message:@"点击确定, 系统会给您发送一封邮件, 请前往重置密码" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSString *username = alert.textFields[0].text;
        if (username.length) {

            BmobQuery *query = [BmobUser query];
            [query whereKey:@"username" equalTo:username];
            [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                if (array.count) {
                    [BmobUser requestPasswordResetInBackgroundWithEmail:alert.textFields[0].text];
                    [self showMessage:@"验证成功,请稍后前往邮箱验证"];
                    
                }else {
                    
                    [self showMessage:@"邮箱输入错误,请输入注册时的邮箱"];
                }
            }];
            
            
        }else {
             [self showMessage:@"邮箱不能为空"];
        }
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
    }];
    [alert addAction:action];
    [alert addAction:action1];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入注册时的邮箱";
    }];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)showMessage:(NSString *)message {
    self.hub.labelText = message;
    self.hub.mode = MBProgressHUDModeText;
    [self.hub showAnimated:YES whileExecutingBlock:^{
        sleep(2.0);
    } completionBlock:^{
        [self.hub removeFromSuperViewOnHide];
        self.hub = nil;
    }];
    
}

@end
